package com.westa.smallshop.register;


import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;
import android.widget.ImageView;

import com.westa.smallshop.R;
import com.westa.smallshop.common.BaseFragment;
import com.westa.smallshop.login.LoginActivity;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by cy123 on 2016/9/12.
 */
public class RegisterFragment extends BaseFragment implements RegisterContract.View {

    @BindView(R.id.et_register_s1_phone_number)
    AppCompatEditText etPhoneNumber;
    @BindView(R.id.et_register_s1_password)
    AppCompatEditText etPassword;
    @BindView(R.id.et_register_s1_password_again)
    AppCompatEditText etPasswordAgain;
    @BindView(R.id.iv_delete_phone_number)
    ImageView ivDeletePhoneNumber;
    @BindView(R.id.iv_delete_password)
    ImageView ivDeletePassword;
    @BindView(R.id.iv_delete_password_repeat)
    ImageView ivDeletePasswordRepeat;

    @NonNull
    private RegisterContract.Presenter mPresenter;

    public static RegisterFragment newInstance() {
        return new RegisterFragment();
    }

    @Override
    public void initView() {
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.destroy();
    }

    @Override
    public int getCreateViewLayoutRes() {
        return R.layout.fragment_register;
    }

    @Override
    public void showPhoneNumberEmptyError() {
        showSingleToast(R.string.tst_phone_number_empty);
    }

    @Override
    public void showPasswordEmptyError() {
        showSingleToast(R.string.tst_password_empty);

    }

    @Override
    public void showPasswordRepeatEmptyError() {
        showSingleToast(R.string.tst_password_repeat_empty_error);
    }

    @Override
    public void showPasswordTooShort() {
        showSingleToast(R.string.tst_password_too_short);
    }

    @Override
    public void showPasswordNotSameError() {
        showSingleToast(R.string.tst_password_not_same);
    }

    @Override
    public void showPhoneNumberAlreadyExistsError() {
        showSingleToast(R.string.tst_phone_number_already_exists);
    }

    @Override
    public void showCreateSuccessful() {

//        RegisterSecondStepActivity.startActivity(getActivity());
        getActivity().finish();
    }

    @Override
    public void showCreateFailed(String errorMsg) {
        showSingleToast(errorMsg);
    }

    @Override
    public void setPresenter(@NonNull RegisterContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }


    @OnClick(R.id.btn_register_s1_create_now)
    public void onCreateButtonClick() {
        String phoneNumber = etPhoneNumber.getText().toString();
        String password = etPassword.getText().toString();
        String passwordRepeat = etPasswordAgain.getText().toString();
        boolean checked = mPresenter.doCheckInfo(phoneNumber, password, passwordRepeat);
        if (checked) {
            mPresenter.doCreate(phoneNumber, password);
        }

    }

    @Override
    public void showRegisterDialogProgress() {
        showDialogProgress(R.string.dlg_register_hint, new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mPresenter.cancelRegister();
            }
        });
    }

    @OnClick({R.id.iv_delete_password, R.id.iv_delete_phone_number, R.id.iv_delete_password_repeat})
    public void onDeleteThingsClick(View view) {
        switch (view.getId()) {
            case R.id.iv_delete_phone_number:
                etPhoneNumber.setText("");
                break;
            case R.id.iv_delete_password:
                etPassword.setText("");
                break;
            case R.id.iv_delete_password_repeat:
                etPasswordAgain.setText("");
                break;
        }
    }

    @OnTextChanged(R.id.et_register_s1_phone_number)
    public void onPhoneNumberTextChanged(CharSequence s, int start, int before, int count) {
        ivDeletePhoneNumber.setVisibility(s.length() > 0 ? View.VISIBLE : View.INVISIBLE);
    }

    @OnTextChanged(R.id.et_register_s1_password)
    public void onPasswordTextChanged(CharSequence s, int start, int before, int count) {
        ivDeletePassword.setVisibility(s.length() > 0 ? View.VISIBLE : View.INVISIBLE);
    }

    @OnTextChanged(R.id.et_register_s1_password_again)
    public void onPasswordRepeatTextChanged(CharSequence s, int start, int before, int count) {
        ivDeletePasswordRepeat.setVisibility(s.length() > 0 ? View.VISIBLE : View.INVISIBLE);
    }

    @OnClick(R.id.tv_forget_password)
    public void onClick() {
        LoginActivity.startActivity(getActivity());
    }
}
